package eqpkanban

import grails.converters.JSON

import java.text.SimpleDateFormat

/**
 * @Program: eqpkanban
 * @Description: 设备看板
 * @Author: 樊双峰
 * @Date: 2018-07-03 22:40
 */
class EqpBoardController {

    def dynamicQueryService

    /**
     * 获取设备信息
     * @return
     */
    def getEqpInfoJson() {
        params.SYS_QUERY_NAME = 'getEqpInfoJson'
        def getShape = dynamicQueryService.queryForList(params)
        render getShape as JSON
    }

    /**
     * 添加设备
     * @param cfu
     * @return
     */
    def saveEqpInfo(EqpShape eqpShape) {
        eqpShape.save()
        render """{
                    "statusCode":"200",
                    "message":"${
            message(code: 'default.qcreated.message',
                    args: [
                            message(code: 'EqpShape.label', default: 'EqpShape'),
                            eqpShape.id
                    ]
            )
        }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }

    /**
     * 删除设备
     * @param c
     * @return
     */
    def deleteEqpInfo(EqpShape eqpShape) {
        eqpShape.delete()
        render """{
                    "statusCode":"200",
                    "message":"${
            message(code: 'default.created.message',
                    args: [
                            message(code: 'EqpShape.label', default: 'EqpShape'),
                            eqpShape.id
                    ]
            )
        }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }

    /**
     * 拖拽
     * @return
     */
    def changeLocation() {
        def id = params.id ?: 0
        def eqpShape = EqpShape.get(id)
        eqpShape.x = new Double(params.x)
        eqpShape.y = new Double(params.y)
        try {
            p.save(flush: true)
            if (eqpShape.hasErrors()) {
                println eqpShape.errors
            }
        } catch (Exception e) {
            print(e.getMessage())
        }
        render ""
    }

    /**
     * 旋转
     * @return
     */
    def changeRotation() {
        def id = params.id ?: 0
        def eqpShape = EqpShape.get(id)
        eqpShape.rotation = new String(params.rotation)
        try {
            eqpShape.save(flush: true)
            if (eqpShape.hasErrors()) {
                println eqpShape.errors
            }
        } catch (Exception e) {
            print(e.getMessage())
        }
        render ""
    }

    /**
     * 编辑图元的大小和位置
     * @return
     */
    def editRectShapeInfo() {
        def id = params.id ?: 0
        def eqpShape = EqpShape.get(id)
        eqpShape.x = new Double(params.x)
        eqpShape.y = new Double(params.y)
        eqpShape.width = new Double(params.width)
        eqpShape.height = new Double(params.height)
        try {
            eqpShape.save(flush: true)
            if (eqpShape.hasErrors()) {
                println eqpShape.errors
            }
        } catch (Exception e) {
            print(e.getMessage())
        }
        render ""
    }

    /**
     * 移除
     * @return
     */
    def removeNode() {
        def id = params.id ?: 0
        def eqpShape = EqpShape.get(id)
        eqpShape.delete()
    }

    /**
     * 设备信息展示页面
     * @return
     */
    def eqpInfo() {
        // 设置编码集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 设置分页
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def user = params.lmUser
        params.SYS_QUERY_NAME = 'areaList'
        def areaList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'eqpList'
        def eqpList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'getEqpInfo'
        def getEqpInfo = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'eqpCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        [pageSize: pageSize, totalCount: totalCount, pageSizeList: pageSizeList,
         getEqpInfo: getEqpInfo, eqpList: eqpList, areaList: areaList, user: user,
         eqpId: params.eqpId?:'', areaName: params.areaName?:'',startNo: params.startNo,endNo: params.endNo]
    }

    /**
     * 增加设备页面
     * @param s
     * @return
     */
    def addEqp(EqpShape eqpShape, Graph graph, Port port) {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        params.SYS_QUERY_NAME = 'areaList'
        def areaList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'eqpList'
        def eqpList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'graphList'
        def graphList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'portList'
        def portList = dynamicQueryService.queryForList(params)
        render(view: 'addEqp',
                model: [EqpShape: eqpShape, Graph: graph, Port: port,
                        eqpList: eqpList, graphList: graphList, portList: portList, areaList: areaList]
        )
    }

    /**
     * 修改设备页面
     * @param s
     * @return
     */
    def editEqp(EqpShape eqpShape) {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        params.SYS_QUERY_NAME = 'graphList'
        def graphList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'portList'
        def portList = dynamicQueryService.queryForList(params)
        render(view: 'editEqp',
                model: [EqpShape: eqpShape, graphList: graphList, portList: portList])
    }

    /**
     * 看板维护页面
     * @return
     */
    def editEqpBoard() {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        params.SYS_QUERY_NAME = 'areaList'
        def areaList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = "getStateColor"
        def getStateColor = dynamicQueryService.queryForList(params)
        [getStateColor: getStateColor, areaList: areaList]
    }

    /**
     * 看板展示页面
     * @return
     */
    def showEqpBoard() {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        params.SYS_QUERY_NAME = 'areaList'
        def areaList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = "getStateColor"
        def getStateColor = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = "getStateCount"
        def getStateCount = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'getEqpInfoJson'
        def eqpInfoList = dynamicQueryService.queryForList(params)
        [
                getStateColor: getStateColor,
                areaList     : areaList,
                getStateCount: getStateCount,
                eqpInfoList  : eqpInfoList
        ]
    }
}